let mongoose = require('mongoose');



// let schema = new mongoose.Schema({
// },{versionKey:false});

module.exports= {
    async list(option){
        let time = Date.now()-3*24*60*60*1000
        let Record = mongoose.models['Record'];
        let records = await Record.getRecord(option.self._id,new Date(time));
        return records
    },
    async people(option){
        let startLen = option.idx ? parseInt(option.idx) : 0
        let Record = mongoose.models['Record'];
        let records = await Record.find({users:{$elemMatch:{_id:option.self._id}}},null,{sort:{endTime:-1}})
        let usrs = [];
        let GameUserModel = mongoose.models['GameUser']
        while(usrs.length < startLen+6 && records.length){
            for(let j=0; j<records.length; j++){
                let record = records[j]
                let _id =record._id.toString()
                for(let i =0; i<record.users.length; i++){
                    let user = record.users[i];
                    let gameUser = await GameUserModel.findOne({_id:user._id.toString()}) //检测此用户是否存在
                    if(gameUser && user._id != option.self._id && !usrs.find(function(x){return x._id == user._id})){
                        usrs.push(user);
                    }
                }
                if(usrs.length >= startLen+6){
                    break;
                }
            }
            records.splice(0,1)
        }

        return usrs.splice(startLen,Math.min(startLen+6,usrs.length))
        // return usrs
    }

};

// module.exports = schema;